package com.ruoyi.hospital.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.HosAssignMapper;
import com.ruoyi.hospital.domain.HosAssign;
import com.ruoyi.hospital.service.IHosAssignService;

/**
 * 床位分配信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@Service
@Slf4j
public class HosAssignServiceImpl implements IHosAssignService 
{
    @Autowired
    private HosAssignMapper hosAssignMapper;

    /**
     * 查询床位分配信息
     * 
     * @param assignId 床位分配信息主键
     * @return 床位分配信息
     */
    @Override
    public HosAssign selectHosAssignByAssignId(Long assignId)
    {
        return hosAssignMapper.selectHosAssignByAssignId(assignId);
    }

    /**
     * 查询床位分配信息列表
     * 
     * @param hosAssign 床位分配信息
     * @return 床位分配信息
     */
    @Override
    public List<HosAssign> selectHosAssignList(HosAssign hosAssign)
    {
        return hosAssignMapper.selectHosAssignList(hosAssign);
    }

    /**
     * 新增床位分配信息
     * 
     * @param hosAssign 床位分配信息
     * @return 结果
     */
    @Override
    public int insertHosAssign(HosAssign hosAssign)
    {
        if (!canAssign(hosAssign)) return -1;
        hosAssign.setAssignId(System.currentTimeMillis());
        hosAssign.setCreateTime(DateUtils.getNowDate());
        return hosAssignMapper.insertHosAssign(hosAssign);
    }

    /**
     * 修改床位分配信息
     * 
     * @param hosAssign 床位分配信息
     * @return 结果
     */
    @Override
    public int updateHosAssign(HosAssign hosAssign)
    {
        if (!canAssign(hosAssign)) return -1;
        return hosAssignMapper.updateHosAssign(hosAssign);
    }

    /**
     * 批量删除床位分配信息
     * 
     * @param assignIds 需要删除的床位分配信息主键
     * @return 结果
     */
    @Override
    public int deleteHosAssignByAssignIds(Long[] assignIds)
    {
        return hosAssignMapper.deleteHosAssignByAssignIds(assignIds);
    }

    /**
     * 删除床位分配信息信息
     * 
     * @param assignId 床位分配信息主键
     * @return 结果
     */
    @Override
    public int deleteHosAssignByAssignId(Long assignId)
    {
        return hosAssignMapper.deleteHosAssignByAssignId(assignId);
    }

    private Boolean canAssign(HosAssign hosAssign) {
        Long assignId = hosAssign.getAssignId();
        Long bunkId = hosAssign.getBunkId();
        Date startTime = hosAssign.getStartTime();
        Date endTime = hosAssign.getEndTime();
        if (bunkId == null) {
            log.error("床位id为空，无法插入/修改");
            return false;
        }
        if (startTime == null || endTime == null || startTime.compareTo(endTime) > 0) {
            log.error("开始时间必须小于等于结束时间");
            return false;
        }
        HosAssign temp = new HosAssign();
        // 根据床位id查询床位分配情况
        temp.setBunkId(bunkId);
        List<HosAssign> hosAssignList = hosAssignMapper.selectHosAssignList(temp);
        if (hosAssignList.isEmpty()) {
            // 未查到预约记录，可预约
            return true;
        }
        for (HosAssign assign : hosAssignList) {
            if (assignId != null && assignId.equals(assign.getAssignId())) {
                // 如果是修改操作，则排除本身
                continue;
            }
            if (startTime.compareTo(assign.getEndTime()) > 0 || endTime.compareTo(assign.getStartTime()) < 0) {
                continue;
            }
            else return false;
        }
        return true;
    }
}
